var ok = ': <div style="color:green;">ok.</div><p/>';
var fail = ': <div style="color:red;">failed!</div><p/>';

function show(output, msg, boolean) {
    output.innerHTML += msg;

    if (boolean) {
	output.innerHTML += fail;
    } else {
	output.innerHTML += ok;
    }
}

function runTest(file) {
    var output = document.getElementById('output');

    if (file.name != 'test.bin') {
	output.innerHTML += file.name + ': not test.bin\n';
    }

    function check(e) {
	var reader = new BinaryReader(e.target.result);
	output.innerHTML += file.name + ':\n';

	show(output, 'readUInt8', reader.readUInt8() != 0 || reader.readUInt8() != 0xff);
	show(output, 'readUInt16', reader.readUInt16() != 0 || reader.readUInt16() != 0xffff);
	show(output, 'readUInt32', reader.readUInt32() != 0 || reader.readUInt32() != 0xffffffff);

	var max = Math.pow(2, 7);
	show(output, 'readInt8', reader.readInt8() != -max  || reader.readInt8() != max - 1);
	var max = Math.pow(2, 15);
	show(output, 'readInt16', reader.readInt16() != -max  || reader.readInt16() != max - 1);
	var max = Math.pow(2, 31);
	show(output, 'readInt32', reader.readInt32() != -max  || reader.readInt32() != max - 1);

	show(output, 'readChar', reader.readChar() != 'h');
	show(output, 'readFloat', reader.readFloat() != -1. || reader.readFloat() != 15.);

	var msg = reader.readString(12);

	show(output, 'readString', msg != 'hello salute');
    }

    var reader = new FileReader();
    reader.onload = check;
    reader.onerror = function(e) { alert('failed to load file'); }
    reader.readAsBinaryString(file);
}

function test(fileList) {
    for (var f = 0;f < fileList.length;f++) {
	runTest(fileList[f]);
    }
}
